﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using NC.Cather;
using NC.Infrastructure;
using NC.Infrastructure.Data;
using NC.Infrastructure.Extension;
using NC.Service.CatchManager;
using domainEntity = NC.Cather.UrlList;

namespace NC.Web.Areas.Admin.Controllers
{
    public class UrlListController : Controller
    {
        private readonly NCDbContext _context;
        private readonly ICatchService _catchService;
        public UrlListController(NCDbContext context, ICatchService catchService)
        {
            this._context = context;
            this._catchService = catchService;
        }

        // GET: UrlList/Index
        public ActionResult Index(int targetId = 0)
        {
            try
            {

            }
            catch (Exception)
            {

                throw;
            }
            var urlList = _context.UrlLists.Where(n => n.Target.Id == targetId).ToList();
            ViewBag.targetName = _context.Targets.Where(n => n.Id == targetId).Select(n => n.Name).FirstOrDefault();
            return View(urlList.ToList());
        }

        // GET: UrlList/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: UrlList/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(domainEntity entity)
        {
            var targetId = Convert.ToInt32(Request.Form["targetId"]);
            var target = _context.Targets.Find(targetId);
            entity.Target = target;
            if (Request.Form["EnablePager"] != null)
            {
                entity.EnablePager = true;
            }
            _context.UrlLists.Add(entity);
            _context.Commit();
            return RedirectToAction("Index", new { targetId = targetId });
        }

        // GET: UrlList/Edit/5
        public ActionResult Edit(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            UrlList UrlList = _context.UrlLists.Find(id);
            if (UrlList == null)
            {
                return HttpNotFound();
            }
            return View(UrlList);
        }

        // POST: UrlList/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(domainEntity entity)
        {
            var targetId = Convert.ToInt32(Request.Form["targetId"]);
            var target = _context.Targets.Find(targetId);
            entity.Target = target;
            _context.UrlLists.AddOrUpdate(entity);
            _context.Commit();
            return RedirectToAction("Index", new { targetId = targetId });
        }

        // POST: UrlList/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            string[] ids = id.Split('|');
            int _id;
            foreach (var item in ids)
            {
                _id = Convert.ToInt32(item);
                var _entity = _context.UrlLists.Find(_id);
                _context.UrlLists.Remove(_entity);
            }
            _context.Commit();
            return Json("", JsonRequestBehavior.AllowGet);
        }


        #region Api

        [HttpPost]
        public JsonResult GetContentSelector(string contentSelector, string url, string method = "get")
        {
            string result = _catchService.GetListContainer(url, contentSelector,
                HttpMethodHelper.GetMethodByString(method));
            return Json(new
            {
                status = 1,
                msg = result
            });
        }
        [HttpPost]
        public JsonResult GetASelector(string aSelector, string contentSelector, string url, string method = "get")
        {
            string result = _catchService.GetListATags(url, contentSelector, aSelector,
                HttpMethodHelper.GetMethodByString(method));
            return Json(new
            {
                status = 1,
                msg = result
            });
        }

        //格式Pager数据
        [HttpPost]
        public JsonResult GetPagerSelector(string aSelector, string contentSelector, string url, string method = "get")
        {
            string result = _catchService.GetListATags(url, contentSelector, aSelector,
                HttpMethodHelper.GetMethodByString(method));
            return Json(new
            {
                status = 1,
                msg = result
            });
        }

        [HttpPost]
        public JsonResult CheckPagerIsValid(string pagerSelector)
        {
            bool check = PagerExtension.IsMathPager(pagerSelector);
            if (check)
                return Json(new { status = 1 });
            return Json(new { status = 0, msg = "分页模板格式错误" });
        }
        [HttpPost]
        public JsonResult CheckQueryStringIsValid(string queryString)
        {
            bool check = PagerExtension.IsMatchQueryString(queryString);
            if (check)
                return Json(new { status = 1 });
            return Json(new { status = 0, msg = "queryString参数格式错误" });
        }


        #endregion



        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}